<template>
  <el-dialog
    :title="!dataForm.auId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="usUserName">
<!--      <el-input v-model="dataForm.auUserId" placeholder="用户id"></el-input>-->
      <template>
        <el-select v-model="dataForm.auUserId" placeholder="请选择" >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="作者笔名" prop="auPenName">
      <el-input v-model="dataForm.auPenName" placeholder="作者笔名"></el-input>
    </el-form-item>
    <el-form-item label="作者介绍" prop="auIntroduce">
      <el-input v-model="dataForm.auIntroduce" placeholder="作者介绍"></el-input>
    </el-form-item>
<!--    <el-form-item label="创建时间" prop="auCreationTime">-->
<!--      <el-input v-model="dataForm.auCreationTime" placeholder="创建时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="更新时间" prop="auUpdateTime">-->
<!--      <el-input v-model="dataForm.auUpdateTime" placeholder="更新时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="作者状态" prop="auStatus">-->
<!--      <el-input v-model="dataForm.auStatus" placeholder="作者状态"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          auId: 0,
          auUserId: '',
          auPenName: '',
          auIntroduce: '',
          auCreationTime: '',
          auUpdateTime: '',
          auStatus: '',
        },
        options: [],
        value: '',
        dataRule: {
          auUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          auPenName: [
            { required: true, message: '作者笔名不能为空', trigger: 'blur' }
          ],
          auIntroduce: [
            { required: true, message: '作者介绍不能为空', trigger: 'blur' }
          ],
          // auCreationTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // auUpdateTime: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ],
          // auStatus: [
          //   { required: true, message: '作者状态不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    mounted() {
      this.getUserList();
    },
    methods: {
      getUserList(){
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/fiction/tbuser/getUserList'),
          method: 'get',
          // params: this.$http.adornParams({
          //   'page': this.pageIndex,
          //   'limit': this.pageSize,
          //   'key': this.dataForm.key
          // })
        }).then((data) => {
          if (data && data.code === 200) {
            this.options = data.data
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      init (id) {
        this.dataForm.auId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.auId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbauthor/info/${this.dataForm.auId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.auUserId = data.tbAuthor.auUserId
                this.dataForm.auPenName = data.tbAuthor.auPenName
                this.dataForm.auIntroduce = data.tbAuthor.auIntroduce
                // this.dataForm.auCreationTime = data.tbAuthor.auCreationTime
                // this.dataForm.auUpdateTime = data.tbAuthor.auUpdateTime
                // this.dataForm.auStatus = data.tbAuthor.auStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbauthor/${!this.dataForm.auId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'auId': this.dataForm.auId || undefined,
                'auUserId': this.dataForm.auUserId,
                'auPenName': this.dataForm.auPenName,
                'auIntroduce': this.dataForm.auIntroduce,
                // 'auCreationTime': this.dataForm.auCreationTime,
                // 'auUpdateTime': this.dataForm.auUpdateTime,
                // 'auStatus': this.dataForm.auStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
